﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_quan_ly_tinh_thanh : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetAllProvince();
            GetAllDistrict();
            LoadDataToProvinceDDL();
            LoadDataToDistrictDDL();
            LoadDataTogrvWard();
        }
    }
    protected void GetAllProvince()
    {
        try 
        {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetAllProvince";
            DataTable db = Dal.GetDataTable();
            grvProvince.DataSource = db;
            grvProvince.DataBind();
            ddlProvince.DataSource = db;
            ddlProvince.DataTextField = db.Columns["TenTinhThanhPho"].ToString();
            ddlProvince.DataValueField = db.Columns["TinhThanhPhoID"].ToString();
            ddlProvince.DataBind();
        }
        catch { }
    }
    protected void grvProvince_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvProvince.EditIndex = -1;
        GetAllProvince();
    }
    protected void grvProvince_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string i = Convert.ToString(grvProvince.DataKeys[e.RowIndex].Value);
        try 
        {
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_DeleteProvince";
            Dal.ParameterCollection = new string[1] { "@TinhThanhPhoID" };
            Dal.ValueCollection = new string[1] { i};
            Dal.ExecuteNonQuery();
            Dal.Close();
            Dal.Dispose();
            GetAllProvince();
        }
        catch { }
    }
    protected void grvProvince_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvProvince.EditIndex = e.NewEditIndex;
        GetAllProvince();
        ((TextBox)grvProvince.Rows[e.NewEditIndex].Cells[0].Controls[0]).Enabled = true;
    }
    protected void grvProvince_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string i = Convert.ToString(grvProvince.DataKeys[e.RowIndex].Value);
        string mota=((TextBox)(grvProvince.Rows[e.RowIndex].Cells[0].Controls[0])).Text;
        try 
        {
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_UpdateProvince";
            Dal.ParameterCollection = new string[2] { "@TinhThanhPhoID", "@TenTinhThanhPho" };
            Dal.ValueCollection = new string[2] { i,mota};
            Dal.ExecuteNonQuery();
            Dal.Close();
            Dal.Dispose();
            grvProvince.EditIndex = -1;
            GetAllProvince();
        }
        catch { }
    }
    protected void btnAdProvince_Click(object sender, EventArgs e)
    {
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_AddProvince";
            Dal.ParameterCollection = new string[1] { "@TenTinhThanhPho" };
            Dal.ValueCollection = new string[1] { txtProvince.Text};
            Dal.ExecuteNonQuery();
            Dal.Close();
            Dal.Dispose();
            GetAllProvince();
        }
        catch { }
    }
    protected void btnAdDistrict_Click(object sender, EventArgs e)
    {
        try 
        {
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_AddDistrict";
            Dal.ParameterCollection = new string[2] { "@TinhThanhPhoID", "@TenHuyenQuan" };
            Dal.ValueCollection = new string[2] { ddlProvince.SelectedValue,txtDistrictName.Text};
            Dal.ExecuteNonQuery();
            Dal.Close();
            Dal.Dispose();
            GetAllDistrict();
        }
        catch { }
    }
    protected void GetAllDistrict()
    {
        try
        {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetAllDistrict";
            DataTable db = Dal.GetDataTable();
            grvDistrict.DataSource = db;
            grvDistrict.DataBind();
        }
        catch { }
    }
    protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetDistrictByProvinceID();
    }
    protected void GetDistrictByProvinceID()
    { 
        //try
        //{
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetDistrictByProvinceID";
            Dal.ParameterCollection = new string[1] { "@TinhThanhPhoID" };
            Dal.ValueCollection = new string[1] { ddlProvince.SelectedValue};
            DataTable db = Dal.GetDataTable();
            grvDistrict.DataSource = db;
            grvDistrict.DataBind();
        //}
        //catch{}
    }
    protected void grvDistrict_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvDistrict.EditIndex = -1;
        GetAllDistrict();
    }
    protected void grvDistrict_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string i = Convert.ToString(grvDistrict.DataKeys[e.RowIndex].Value);
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_DeleteDistrict";
            Dal.ParameterCollection = new string[1] { "@HuyenQuanID" };
            Dal.ValueCollection = new string[1] { i};
            Dal.ExecuteNonQuery();
            Dal.Close();
            Dal.Dispose();
            GetAllDistrict();
        }
        catch { }
    }
    protected void grvDistrict_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvDistrict.EditIndex = e.NewEditIndex;
        GetAllDistrict();
    }
    protected void grvDistrict_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox districtName = (TextBox)grvDistrict.Rows[e.RowIndex].FindControl("txtDistrictName");
        DropDownList provinceName = (DropDownList)grvDistrict.Rows[e.RowIndex].FindControl("ddlProvinceName");
        string i = Convert.ToString(grvDistrict.DataKeys[e.RowIndex].Value);
        try {
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "[SP_UpdateDistrict]";
            Dal.ParameterCollection = new string[3] { "@TinhThanhPhoID", "@TenHuyenQuan", "@HuyenQuanID" };
            Dal.ValueCollection = new string[3] { provinceName.SelectedValue,districtName.Text,i};
            Dal.ExecuteNonQuery();
            Dal.Close();
            Dal.Dispose();
            grvDistrict.EditIndex = -1;
            GetAllDistrict();

        }
        catch { }
    }
    protected void grvDistrict_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //try
        //{
            DataRowView drv = e.Row.DataItem as DataRowView;
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                SQLHelper Dal = new SQLHelper();
                Dal.CommandType = CommandType.StoredProcedure;
                Dal.CommandText = "SP_GetAllProvince";
                DataTable db = Dal.GetDataTable();
                DropDownList ddlProvince = (DropDownList)e.Row.FindControl("ddlProvinceName");
                ddlProvince.DataSource = db;
                ddlProvince.DataTextField = db.Columns["TenTinhThanhPho"].ToString();
                ddlProvince.DataValueField = db.Columns["TinhThanhPhoID"].ToString();
                ddlProvince.SelectedValue = drv[2].ToString();
                ddlProvince.DataBind();
                
            }
        //}
        //catch
        //{ }
    }
    protected void grvWard_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {

    }
    protected void grvWard_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }
    protected void grvWard_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void grvWard_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    protected void grvWard_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

    }
    protected void LoadDataToProvinceDDL()
    {
        try 
        {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetAllProvince";
            DataTable db = Dal.GetDataTable();
            ddlProvince1.DataSource = db;
            ddlProvince1.DataTextField = db.Columns["TenTinhThanhPho"].ToString();
            ddlProvince1.DataValueField = db.Columns["TinhThanhPhoID"].ToString() ;
            ddlProvince1.DataBind();
        }
        catch { }
    }
    protected void LoadDataToDistrictDDL()
    {
        try
        {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetAllDistrict";
            DataTable db = Dal.GetDataTable();
            ddlDistrict.DataSource = db;
            ddlDistrict.DataTextField = db.Columns["TenHuyenQuan"].ToString();
            ddlDistrict.DataValueField = db.Columns["HuyenQuanID"].ToString();
            ddlDistrict.DataBind();
        }
        catch
        { }
    }
    protected void ddlProvince1_SelectedIndexChanged(object sender, EventArgs e)
    {
        try 
        {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetDistrictByProvinceID";
            Dal.ParameterCollection = new string[1] { "@TinhThanhPhoID" };
            Dal.ValueCollection = new string[1] { ddlProvince1.SelectedValue};
            DataTable db = Dal.GetDataTable();
            ddlDistrict.DataSource = db;
            ddlDistrict.DataTextField = db.Columns["TenHuyenQuan"].ToString();
            ddlDistrict.DataValueField = db.Columns["HuyenQuanID"].ToString();
            ddlDistrict.DataBind();

        }
        catch { }
    }
    protected void btnAddWard_Click(object sender, EventArgs e)
    {
        try 
        {
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_AddWard";
            Dal.ParameterCollection = new string[2] { "@HuyenQuanID", "@TenXaPhuong" };
            Dal.ValueCollection = new string[2] { ddlDistrict.SelectedValue,txtWardName.Text};
            Dal.ExecuteNonQuery();
            Dal.Close();
            Dal.Dispose();
        }
        catch { }
    }
    protected void LoadDataTogrvWard()
    {
        try 
        {
            SQLHelper Dal = new SQLHelper();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_GetAllWard";
            DataTable db = Dal.GetDataTable();
            grvWard.DataSource = db;
            grvWard.DataBind();
        }
        catch { }
    }
    
}
